package temaihui.daimaitemai;

import android.content.Intent;
import android.graphics.Paint;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.json.JSONObject;
import org.w3c.dom.Text;

import temaihui.daimaitemai.GoodItemDetailModel;
import temaihui.daimaitemai.R;
import temaihui.daimaitemai.RequestAction;
import temaihui.daimaitemai.adapter.ItemDetailGridAdapter;

public class GoodDetailActivity extends FragmentActivity {

    private int itemId;
    private GoodItemModel itemDetailModel;
    private GridView gridView;
    private SlideShowView bannerView;
    private TextView freeShipTextView;
    private TextView nameTextView;
    private TextView sellingPriceTextView;
    private TextView discountTextView;
    private TextView descTextView;
    private ImageButton backButton;
    private Button gotoDetailButton;

    private TextView originPriceTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_detail);
        bannerView =(SlideShowView) findViewById(R.id.item_detail_banner);
        gridView = (GridView) findViewById(R.id.item_detail_grid);
        freeShipTextView = (TextView)findViewById(R.id.item_detail_free_ship);
        nameTextView = (TextView)findViewById(R.id.item_detail_name);
        sellingPriceTextView = (TextView)findViewById(R.id.item_detail_selling_price);
        descTextView = (TextView)findViewById(R.id.item_detail_desc);
        originPriceTextView = (TextView)findViewById(R.id.item_detail_origin_price);
        discountTextView = (TextView)findViewById(R.id.item_detail_origin_discount);
        backButton = (ImageButton)findViewById(R.id.item_detail_back);
        gotoDetailButton = (Button)findViewById(R.id.btn_go_to_taobao);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        gotoDetailButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToDetail();
            }
        });

        itemId = getIntent().getIntExtra("itemId",0);
        getData();
    }
    private void getData() {
        JSONObject data = new JSONObject();
        try {
            data.put("userId","");
            data.put("goodsId",itemId);
        } catch (Exception e) {

        }
        RequestAction.sendRequest(this, "appserver/getGoodsDetail", data, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                renderViewByResponse(jsonObject);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
    }
    private void goToDetail(){
        AliUtil.getInstance().showDetail(itemDetailModel.getData().getGoodsTaobaoId(),this);
    }
    private void renderViewByResponse(JSONObject jsonObject){
        try {
            itemDetailModel = JSONHelper.parseObject(jsonObject, GoodItemModel.class);
            String[] imageUrls = new String[itemDetailModel.getData().getGoodsPicList().size()];
            for (int i=0; i< itemDetailModel.getData().getGoodsPicList().size();i++){
                imageUrls[i] = itemDetailModel.getData().getGoodsPicList().get(i).getPic();
            }
            bannerView.beginWork(true, imageUrls);
            ItemDetailGridAdapter adapter = new ItemDetailGridAdapter(this,itemDetailModel.getData().getGoodsRecomList());
            gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int itemId = itemDetailModel.getData().getGoodsRecomList().get(position).getId();
                    Intent intent = new Intent(GoodDetailActivity.this, GoodDetailActivity.class);
                    intent.putExtra("itemId", itemId);
                    startActivity(intent);

                }
            });

            sellingPriceTextView.setText("￥" + String.valueOf(itemDetailModel.getData().getSellingPrice()));

            originPriceTextView.setText("￥" + String.valueOf(itemDetailModel.getData().getOriginalPrice()));
            originPriceTextView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

            discountTextView.setText(String.valueOf(itemDetailModel.getData().getDiscount())+"折" );
            nameTextView.setText(itemDetailModel.getData().getGoodsName() );
            freeShipTextView.setText(itemDetailModel.getData().getFreeShip());

            descTextView.setText(itemDetailModel.getData().getDesc());

        }catch (Exception e){

        }

    }
}
